package com.zhongxi.guoyuan.view.coinShopping;

import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.CoinShopDetailData;
import com.zhongxi.guoyuan.entity.coinStore.CoinShopDetailResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.MainHomeActivity;

import java.util.HashMap;
import java.util.Map;

import static android.R.attr.password;
import static com.zhongxi.guoyuan.R.id.tvSurePay;

public class CoinCheckstandActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvPay;
    private EditText etCoin;
    private TextView tvCoinRest;
    private EditText etPassword;
    private String shopid;
    private String type;
    private ImageView ivImg;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvAddress;
    private Map<String, String> map = new HashMap<String, String>();
    private String coin;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_coin_checkstand);
        initTitle();
        tvTitle.setText("收银台");
        shopid = getIntent().getStringExtra("shopid");
        tvPay = (TextView) findViewById(R.id.tvPay);
        tvCoinRest = (TextView) findViewById(R.id.tvCoinRest);
        etCoin = (EditText) findViewById(R.id.etCoin);
        etPassword = (EditText) findViewById(R.id.etPassword);
        ivImg = (ImageView) findViewById(R.id.ivImg);
        tvName = (TextView) findViewById(R.id.tvName);
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvAddress = (TextView) findViewById(R.id.tvAddress);
        if(null!=MyApplication.getInstance().getSilver()&&!"".equals(MyApplication.getInstance().getSilver()))
            tvCoinRest.setText("银币余量："+MyApplication.getInstance().getSilver());
    }

    @Override
    protected void initData() {
        super.initData();
        type = "detail";
        String str = "&shopid="+shopid;
        getServer(Url.SHOP_DETAILS, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        if("detail".equals(type)){
            CoinShopDetailResult result = gson.fromJson(response, CoinShopDetailResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            if(null!=result.getShopInfo()&&!"".equals(result.getShopInfo()))
                initStoreData(result.getShopInfo());
        }else if("topay".equals(type)){
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            showShortToast("支付成功");
            showPopupWindow();
        }
    }

    private void showPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_coin_pay, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvPay_Money= (TextView) layout.findViewById(R.id.tvPay_Money);
        TextView tvSurePay = (TextView) layout.findViewById(R.id.tvSure);
        ImageView ivCancel = (ImageView) layout.findViewById(R.id.ivCancel);
        tvPay_Money.setText(coin);
        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startAct(MainHomeActivity.class);
                finish();
            }
        });
        tvSurePay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startAct(MainHomeActivity.class);
                finish();
            }
        });
    }

    /**
     * 初始化店铺详情数据
     * @param shopInfo
     */
    private void initStoreData(CoinShopDetailData shopInfo) {
        if(null!=shopInfo.getImages()&&shopInfo.getImages().size()>0){
//            ImageLoader.getInstance().displayImage(Url.IMG_URL+shopInfo.getImages().get(0), ivImg, CommonTool.getOptions(R.mipmap.iv_eg));
            Glide.with(context).load(Url.IMG_URL+ shopInfo.getImages().get(0)).placeholder(R.mipmap.iv_eg).into(ivImg);
        }
        if(null!=shopInfo.getName()&&!"".equals(shopInfo.getName())){
            tvName.setText(shopInfo.getName());
        }else{
            tvName.setText("");
        }
        if(null!=shopInfo.getPhone()&&!"".equals(shopInfo.getPhone())){
            tvPhone.setText(shopInfo.getPhone());
        }else{
            tvPhone.setText("");
        }
        if(null!=shopInfo.getAddress()&&!"".equals(shopInfo.getAddress())){
            tvAddress.setText(shopInfo.getAddress());
        }else{
            tvAddress.setText("");
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
            if("topay".equals(type)){
                if(1001==result.getCode()){
                    showShortToast("用户未登录");
                }else if(6006==result.getCode()){
                    showShortToast("支付失败");
                }else if(6007==result.getCode()){
                    showShortToast("银币不足");
                }else if(7003==result.getCode()){
                    showShortToast("密码错误");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tvPay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvPay:
                toPay();
                break;
        }
    }

    private void toPay() {
        type = "topay";
        coin = etCoin.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        if(TextUtils.isEmpty(coin)){
            showShortToast("银币不能为空");
        }else if(TextUtils.isEmpty(password)){
            showShortToast("密码不能为空");
        }else{
            map.clear();
            map.put("shopid",shopid);
            map.put("price", coin);
            map.put("pwd", MyApplication.getInstance().descsc(password));
            getServer(Url.COIN_PAY, map, "", false, null, "post");
        }
    }
}
